* Create Figure A.6 and Table A.5

* Prepare data to select episode number
preserve
	use "temp/Depreciation Events.dta", clear
	ren devaluationYear year
	sort ccode year
	save "temp/Depreciation Events_forEventStudy.dta", replace
restore

preserve
	merge m:1 ccode using "temp/Numb Events by Country.dta"
	drop _merge
	merge 1:1 ccode year using "temp/Depreciation Events_forEventStudy.dta"
	drop app* deva 
	quietly do "Analysis_dofiles/Adjustments_EventStudy.do"
	* Create required variables
	sort id year
	quietly do "Analysis_dofiles/CreateVars_EventStudy.do"
	xi: quietly regress ratio devaf5 devaf4 devaf3 devaf2 devaf1 app deval1 deval2 deval3 deval4 deval5 i.ccode i.year i.year*D i.year*Recent,r
	estimates store One
	esttab One, keep(devaf* app deval*) b(%9.3f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01) // devaf* are the pre-deva coeffs, while deval* are post deva ones
	
	local sig = 0.11
	local LHS1 = "ratio"
	local LHS2 = "rer"
	local color1 = "blue"
	local color2 = "red"
	set graphics on
	gen Years = _n-6 if _n <= 11
	do "Analysis_dofiles/eventstudygraph.do" `sig' `LHS1' `color1'

	xi: quietly regress lRER devaf5 devaf4 devaf3 devaf2 devaf1 app deval1 deval2 deval3 deval4 deval5 i.ccode i.year i.year*D i.year*Recent,r
	
	do "Analysis_dofiles/eventstudygraph.do" `sig' `LHS2' `color2'

	tw (line up_ES_`LHS1' Years if _n <= 11, ///
	 fcolor(gs12) lc(`color1') lpattern(shortdash) lw(vthin)) ///
	(line dn_ES_`LHS1' Years if _n <= 11, ///
	 fcolor(gs12) lc(`color1') lpattern(shortdash) lw(vthin)) ///
	(line b_ES_`LHS1' Years if _n <= 11, lcolor(`color1') lpattern(solid) lw(thick)) ///
	(line up_ES_`LHS2' Years if _n <= 11, ///
	 fcolor(gs12) lc(`color2') lpattern(shortdash) lw(vthin)) ///
	(line dn_ES_`LHS2' Years if _n <= 11, ///
	 fcolor(gs12) lc(`color2') lpattern(shortdash) lw(vthin)) ///
	(scatter b_ES_`LHS2' Years if _n <= 11, c(l) color(`color2') ms(X) msize(medlarge) ///
	lpattern(solid) lwidth(thick)), /// 	
	yline(0,lc(black)) xline(0,lc(black) lpattern(dash)) ///
	yt("Coefficient") xtitle("Year Since Devaluation") ///
	graphregion(margin(l=0 r=0 b=0 t=0)) graphregion(color(white)) ///
	xlabel(-5(1)5) ///
	text(-0.25 -1  "RER", color(red) size(small)) /// 
	text(0.15 -1 "Import" "Share", color(blue) size(small)) /// 
	legend(off) scale(1.2) name(ES1, replace)
	qui graph export "`1'/FA6.pdf", replace

	* Regressions for Table A.5
	label var ratio "log(SI)"
	set matsize 11000
	xi: quietly regress ratio deva   i.ccode i.year, r // year FE
	estimates store One
	xi: quietly regress ratio deva   i.ccode year, r // linear trend
	estimates store Two
	xi: quietly regress ratio deva   i.ccode year year2, r // quadratic trend
	estimates store Three
	xi: quietly regress ratio deva   i.ccode year year80 year90 year00, r // decadal-specific, linear
	estimates store Four
	xi: quietly regress ratio deva   i.ccode*year, r // country-specific linear trend
	estimates store Five
	xi: quietly regress ratio deva   i.ccode*year i.ccode*year2, r // country-specific quadratic trend
	estimates store Six
	xi: quietly regress ratio deva   i.ccode*year i.country*year80 i.country*year90 i.country*year00 i.country , r // decadal-specific, country-specific linear
	estimates store Seven
	xi: quietly regress ratio deva   i.ccode i.year i.year*D i.year*Recent, r // year FE, interacted with treated
	estimates store Eight
	xi: quietly regress ratio deva   i.ccode year yearD yearJ year80 year80D year80J year90 year90D year90J year00 year00D year00J, r // decadal-spec, treated
	estimates store Nine	
	esttab One Two Three Four Five Six Seven Eight Nine using "`1'/TableA5_A.csv", label keep(deva) b(%9.2f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01) replace
	
	* Add trend window for 5 years
	label var trendwindow5 "Window Trend"
	xi: quietly regress ratio deva  trendwindow5 i.ccode i.year, r // year FE
	estimates store One
	xi: quietly regress ratio deva  trendwindow5 i.ccode year, r // linear trend
	estimates store Two
	xi: quietly regress ratio deva  trendwindow5 i.ccode year year2, r // quadratic trend
	estimates store Three
	xi: quietly regress ratio deva  trendwindow5 i.ccode year year80 year90 year00, r // decadal-specific, linear
	estimates store Four
	xi: quietly regress ratio deva  trendwindow5 i.ccode*year, r // country-specific linear trend
	estimates store Five
	xi: quietly regress ratio deva  trendwindow5 i.ccode*year i.ccode*year2, r // country-specific quadratic trend
	estimates store Six
	xi: quietly regress ratio deva  trendwindow5 i.ccode*year i.country*year80 i.country*year90 i.country*year00 i.country , r // decadal-specific, country-specific linear
	estimates store Seven
	xi: quietly regress ratio deva  trendwindow5 i.ccode i.year i.year*D i.year*Recent, r // year FE, interacted with treated
	estimates store Eight
	xi: quietly regress ratio deva  trendwindow5 i.ccode year yearD yearJ year80 year80D year80J year90 year90D year90J year00 year00D year00J, r // decadal-spec, treated
	estimates store Nine	
	esttab One Two Three Four Five Six Seven Eight Nine using "`1'/TableA5_B.csv", label keep(deva  trendwindow5) b(%9.2f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01) replace

	* 10 year
	label var trendwindow10 "Window Trend"

	xi: quietly regress ratio deva  trendwindow10 i.ccode i.year, r // year FE
	estimates store One
	xi: quietly regress ratio deva  trendwindow10 i.ccode year, r // linear trend
	estimates store Two
	xi: quietly regress ratio deva  trendwindow10 i.ccode year year2, r // quadratic trend
	estimates store Three
	xi: quietly regress ratio deva  trendwindow10 i.ccode year year80 year90 year00, r // decadal-specific, linear
	estimates store Four
	xi: quietly regress ratio deva  trendwindow10 i.ccode*year, r // country-specific linear trend
	estimates store Five
	xi: quietly regress ratio deva  trendwindow10 i.ccode*year i.ccode*year2, r // country-specific quadratic trend
	estimates store Six
	xi: quietly regress ratio deva  trendwindow10 i.ccode*year i.country*year80 i.country*year90 i.country*year00 i.country , r // decadal-specific, country-specific linear
	estimates store Seven
	xi: quietly regress ratio deva  trendwindow10 i.ccode i.year i.year*D i.year*Recent, r // year FE, interacted with treated
	estimates store Eight
	xi: quietly regress ratio deva  trendwindow10 i.ccode year yearD yearJ year80 year80D year80J year90 year90D year90J year00 year00D year00J, r // decadal-spec, treated
	estimates store Nine	
	esttab One Two Three Four Five Six Seven Eight Nine using "`1'/TableA5_C.csv", label keep(deva  trendwindow10) b(%9.2f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01) replace
restore
